/*
 * @(#)HtmlUbb.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.tag;

import java.util.*;

/**
 * HtmlUbb
 * 
 * @author BIS 2012-12-24
 */
public class HtmlUbb
{
    /**
     * HtmlUbb
     */
    public HtmlUbb()
    {
    }

    /**
     * htmlEntities
     * @Description:
     * @param str
     * @return
     */
    public static String htmlEntities(String str)
    {
        return UBB(str);
    }

    /**
     * hasSpecialCharacter
     * @Description:
     * @param str
     * @return
     */
    public static boolean hasSpecialCharacter(String str)
    {
        /*
        if(str.indexOf("\\")!=-1){
         return false;
        }
        if(str.indexOf("��")!=-1){
         return false;
        }
        if(str.indexOf(��"��)!=-1){
         return false;
        }
        if(str.indexOf("<")!=-1){
         return false;
        }
        if(str.indexOf(">")!=-1){
         return false;
        }
        */
        return true;

    }

    /**
     * UBB
     * @Description:
     * @param fString
     * @return
     */
    public static String UBB(String fString)
    {
        return (ChkString(FormatStr(fString)));
    }

    /**
     * FormatStr
     * @Description:
     * @param fString
     * @return
     */
    public static String FormatStr(String fString)
    {
        /*
        fString = repstr(fString, "��","��");
        fString = repstr(fString, "\"","��");
        fString = repstr(fString, "��","<");
        fString = repstr(fString, "��","<");
        fString = repstr(fString, "��",">");
        fString = repstr(fString, " ","\r");
        */
        fString = repstr(fString, "&acute;", "��");
        fString = repstr(fString, "&quot;", "\"");
        fString = repstr(fString, "&lt;", "<");
        fString = repstr(fString, "&gt;", ">");
        fString = repstr(fString, "&nbsp;", " ");
        fString = repstr(fString, " ", "\r");
        return (fString);
    }

    /**
     * ChkString
     * @Description:
     * @param fString
     * @return
     */
    public static String ChkString(String fString)
    {
        fString = fString.trim();
        if (fString.equals(""))
            fString = " ";
        else
        {
            fString = doCode(fString, "<B>", "</B>", "<b>", "</b>");
            fString = doCode(fString, "<u>", "</u>", "<u>", "</u>");
            fString = doCode(fString, "<I>", "</I>", "<i>", "</i>");
            fString = doCode(fString, "[red]", "[/red]", "<font color=red>", "</font id=red>");
            fString = doCode(fString, "[green]", "[/green]", "<font color=green>", "</font id=green>");
            fString = doCode(fString, "[blue]", "[/blue]", "<font color=blue>", "</font id=blue>");
            fString = doCode(fString, "[white]", "[/white]", "<font color=white>", "</font id=white>");
            fString = doCode(fString, "[purple]", "[/purple]", "<font color=purple>", "</font id=purple>");
            fString = doCode(fString, "[yellow]", "[/yellow]", "<font color=yellow>", "</font id=yellow>");
            fString = doCode(fString, "[violet]", "[/violet]", "<font color=violet>", "</font id=violet>");
            fString = doCode(fString, "[brown]", "[/brown]", "<font color=brown>", "</font id=brown>");
            fString = doCode(fString, "[black]", "[/black]", "<font color=black>", "</font id=black>");
            fString = doCode(fString, "[pink]", "[/pink]", "<font color=pink>", "</font id=pink>");
            fString = doCode(fString, "[orange]", "[/orange]", "<font color=orange>", "</font id=orange>");
            fString = doCode(fString, "[gold]", "[/gold]", "<font color=gold>", "</font id=gold>");
            fString = doCode(fString, "<h1>", "</h1>", "<h1>", "</h1>");
            fString = doCode(fString, "<h2>", "</h2>", "<h2>", "</h2>");
            fString = doCode(fString, "<h3>", "</h3>", "<h3>", "</h3>");
            fString = doCode(fString, "<h4>", "</h4>", "<h4>", "</h4>");
            fString = doCode(fString, "<h5>", "</h5>", "<h5>", "</h5>");
            fString = doCode(fString, "<h6>", "</h6>", "<h6>", "</h6>");
            fString = doCode(fString, "[size=1]", "[/size=1]", "<font size=1>", "</font id=size1>");
            fString = doCode(fString, "[size=2]", "[/size=2]", "<font size=2>", "</font id=size2>");
            fString = doCode(fString, "[size=3]", "[/size=3]", "<font size=3>", "</font id=size3>");
            fString = doCode(fString, "[size=4]", "[/size=4]", "<font size=4>", "</font id=size4>");
            fString = doCode(fString, "[size=5]", "[/size=5]", "<font size=5>", "</font id=size5>");
            fString = doCode(fString, "[size=6]", "[/size=6]", "<font size=6>", "</font id=size6>");
            fString = doCode(fString, "[left]", "[/left]", "<div align=left>", "</div id=left>");
            fString = doCode(fString, "<center>", "</center>", "<center>", "</center>");
            fString = doCode(fString, "<div align=right>", "</div>", "<div align=right>", "</div id=right>");
            fString = doCode(fString, "<pre class=CodeSamp>", "</pre>", "<pre id=code><font face=courier size=3 id=code>", "</font id=code></pre id=code>");
            fString = doCode(fString, "[url]", "[/url]", "<a>", "</a>");
            fString = addlink(fString);
            fString = addBr(fString);
        }
        return (fString);
    }

    /**
     * doCode
     * @Description:
     * @param fString
     * @param fOTag
     * @param fCTag
     * @param fROTag
     * @param fRCTag
     * @return
     */
    public static String doCode(String fString, String fOTag, String fCTag, String fROTag, String fRCTag)
    {
        fString = repstr(fString, fROTag, fOTag);
        fString = repstr(fString, fRCTag, fCTag);
        return (fString);
    }

    /**
     * addlink
     * @Description:
     * @param str
     * @return
     */
    public static String addlink(String str)
    {
        String hrefTagPre = "<a>";
        String hrefTagLast = "</a>";
        String hrefTxt = "";
        String strReturn = "";
        int iSearchedFrom = 0;
        int first = 0;
        int last = 0;
        while ((iSearchedFrom = str.indexOf(hrefTagPre, iSearchedFrom)) != -1)
        {
            first = iSearchedFrom + 3;
            if ((iSearchedFrom = str.indexOf(hrefTagLast, iSearchedFrom)) != -1)
            {
                last = iSearchedFrom;
                iSearchedFrom += 4;
                strReturn = str.substring(first, last);
                if (strReturn.indexOf("<a>", 0) != -1)
                {
                    strReturn = repstr(strReturn, "", "<a>");
                }
                hrefTxt = "<a href =" + strReturn + " target=\"_blank\">" + strReturn + "</a>";
                strReturn = str.substring(first - 3, iSearchedFrom);
                str = repstr(str, hrefTxt, strReturn);
                iSearchedFrom = 0;
            }
            else
                break;
        }
        return (str);
    }
    
    /**
     * repstr
     * @Description:
     * @param str
     * @param strDen
     * @param strSrc
     * @return
     */
    public static String repstr(String str, String strDen, String strSrc)
    {
        int iLen = str.length();
        int iDenLen = strDen.length();
        int iSrcLen = strSrc.length();
        String strReturn = "";
        int iSearchedFrom = 0;
        int iPrevSearchedFrom = 0;
        while ((iSearchedFrom = str.indexOf(strSrc, iSearchedFrom)) != -1)
        {
            strReturn += str.substring(iPrevSearchedFrom, iSearchedFrom);
            strReturn += strDen;
            iSearchedFrom += iSrcLen;
            iPrevSearchedFrom = iSearchedFrom;
        }
        if (iPrevSearchedFrom != 0)
            strReturn += str.substring(str.lastIndexOf(strSrc) + iSrcLen, str.length());
        else
            strReturn = str;
        return (strReturn);
    }

    /**
     * RemoveComment
     * @Description:
     * @param Content
     * @return
     */
    public static String RemoveComment(String Content)
    {
        String makeContent = new String();
        StringTokenizer strToken = new StringTokenizer(Content, "\n");
        String tempToken = null;
        while (strToken.hasMoreTokens())
        {
            tempToken = strToken.nextToken();
            if (tempToken.indexOf(":") != 0)
                makeContent = makeContent + tempToken + "\n";
        }
        return makeContent;
    }

    /**
     * addBr
     * @Description:
     * @param Content
     * @return
     */
    public static String addBr(String Content)
    {
        String makeContent = new String();
        StringTokenizer strToken = new StringTokenizer(Content, "\n");
        while (strToken.hasMoreTokens())
        {
            String sNext = strToken.nextToken();
            makeContent = makeContent + sNext.substring(0, sNext.length() - 1) + "<br>";
        }
        return makeContent;
    }

    /**
     * delBr
     * @Description:
     * @param Content
     * @return
     */
    public static String delBr(String Content)
    {
        String makeContent = new String();
        StringTokenizer strToken = new StringTokenizer(Content, "<br>");
        while (strToken.hasMoreTokens())
        {
            String sNext = strToken.nextToken();
            makeContent = makeContent + sNext.substring(0, sNext.length() - 4) + "\n";
        }
        return makeContent;
    }
}
